<template>
	<el-dialog v-model="dialog" :title="form.id ? '修改字典' : '添加字典'" width="60%" :before-close="handleClose">
		<el-form ref="formRef" :model="form" label-width="120px" :rules="rules" status-icon>
			<el-form-item label="字典标签" prop="dictLabel">
				<el-input v-model="form.dictLabel" show-word-limit maxlength="50"></el-input>
			</el-form-item>
			<el-form-item label="字典键值" prop="dictValue">
				<el-input v-model="form.dictValue" show-word-limit maxlength="100"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remarks">
				<el-input v-model="form.remarks" show-word-limit type="textarea" maxlength="100"></el-input>
			</el-form-item>
			<el-form-item label="排序序号" prop="sort">
				<el-input-number v-model="form.sort" :min="0"></el-input-number>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-radio-group v-model="form.status">
					<el-radio label="0">停用</el-radio>
					<el-radio label="1">正常</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">关 闭</el-button>
				<el-button type="primary" :loading="loading" @click="submitForm">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script lang="ts">
import { defineComponent, ref, reactive, toRefs } from 'vue';
import { addObj, editObj, getById } from '/@/api/upms/dictvalue';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
export default defineComponent({
	props: {
		type: {
			type: String,
			default: 'add',
		},
	},
	emits: ['initPage'],
	setup(props, { emit }) {
		const data = reactive({
			form: { id: undefined, dictLabel: '', dictValue: '', remarks: '', status: '1', sort: 0 },
			rules: {
				dictLabel: [
					{
						required: true,
						message: '请输入字典标签',
						trigger: 'change',
					},
				],
				dictValue: [
					{
						required: true,
						message: '请输入字典键值',
						trigger: 'change',
					},
				],
				status: [
					{
						required: true,
						message: '请选择状态',
						trigger: 'change',
					},
				],
				sort: [
					{
						required: true,
						message: '请输入排序序号',
						trigger: 'change',
					},
				],
			},
		});
		const dialog = ref(false);
		const loading = ref(false);
		const formRef = ref();
		const initForm = (row: any) => {
			if (row && row.id) {
				getDetail(row.id);
			}
			dialog.value = true;
		};
		const getDetail = (id: string) => {
			loading.value = true;
			// 修改
			getById(id)
				.then((response: any) => {
					loading.value = false;
					data.form = response.data.data;
				})
				.catch(() => {
					loading.value = false;
				});
		};
		/**
		 * 关闭事件
		 */
		const handleClose = () => {
			resetForm(formRef.value);
		};
		/**
		 * 重置表单
		 */
		const resetForm = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			loading.value = false;
			dialog.value = false;
			formEl.resetFields();
		};
		/**
		 * 提交按钮
		 */
		const submitForm = async (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			await formEl.validate((valid, fields) => {
				if (valid) {
					loading.value = true;
					if (data.form.id) {
						//修改
						edit();
					} else {
						//新增
						add();
					}
				}
			});
		};
		/**
		 * 新增
		 */
		const add = () => {
			addObj(data.form)
				.then((response: any) => {
					resetForm(formRef.value);
					ElMessage.success('新增成功');
					emit('initPage');
				})
				.catch(() => {
					loading.value = false;
				});
		};
		/**
		 * 修改
		 */
		const edit = () => {
			editObj(data.form)
				.then((response: any) => {
					resetForm(formRef.value);
					ElMessage.success('修改成功');
					emit('initPage');
				})
				.catch(() => {
					loading.value = false;
				});
		};

		return {
			dialog,
			loading,
			formRef,
			...toRefs(data),
			handleClose,
			submitForm,
			initForm,
		};
	},
});
</script>
